package com.example.netty.realNetty.lengthFieldBasedFrameDecoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class LengthFieldBasedFrameDecoder {
    public static void main(String[] args) {
        EmbeddedChannel embeddedChannel = new EmbeddedChannel(
                new io.netty.handler.codec.LengthFieldBasedFrameDecoder(1024,
                        0,4,0,4),
                new LoggingHandler(LogLevel.DEBUG)
        );
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        joint(byteBuf,"hello,word");
        joint(byteBuf,"thank,you");
        embeddedChannel.writeInbound(byteBuf);
    }

    private static void joint(ByteBuf byteBuf, String s) {
        byte[] bytes = s.getBytes();
        int len = bytes.length;
        byteBuf.writeInt(len);
        byteBuf.writeBytes(bytes);
    }

}
